/*
 * Copyright (c) 2014 - 2016. Company.Domain Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.category;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import java.util.List;

import cn.com.dhc.danlu.R;

/**
 * author: 刘立琦(<a href="mailto:liuliqi@danlu.com">liuliqi@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2016-12-01 15:04<br/>
 *
 * <p>
 * 新品类二级分类RecyclerViewAdapter
 * </p>
 */
class ClassifyRecyclerAdapter extends RecyclerView.Adapter<ClassifyRecyclerAdapter.MyViewHolder> {
    private List dataList;
    private LayoutInflater mLayoutInflater;
    private int mCurrentIndex;
    private int mLastIndex;

    public ClassifyRecyclerAdapter(List dataList, int defPosition, Context context) {
        this.dataList = dataList;
        mLayoutInflater = LayoutInflater.from(context);
        this.mCurrentIndex = defPosition;
        this.mLastIndex = defPosition;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(mLayoutInflater.inflate(R.layout.item_classify_layout, parent, false));
    }

    int getCurrentSelectIndex() {
        return mCurrentIndex;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.mRadioButton.setText(dataList.get(position) + "");
        holder.mRadioButton.setChecked(mCurrentIndex == position);
        holder.mRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == mCurrentIndex) return;
                mLastIndex = mCurrentIndex;
                mCurrentIndex = position;
                notifyItemChanged(mCurrentIndex);
                notifyItemChanged(mLastIndex);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        RadioButton mRadioButton;

        MyViewHolder(View itemView) {
            super(itemView);
            mRadioButton = (RadioButton) itemView.findViewById(R.id.radiobutton_item);
        }
    }

}
